package security;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class Generator {

	private static Generator instance;

	public static Generator getInstance() {
		if (instance == null)
			instance = new Generator();
		return instance;
	}

	private Generator() {

	}

	public SecretKey generateSymmetricKey(byte[] password) {
		PBEKeySpec pbeKeySpec;
		SecretKeyFactory keyFac;
		
		pbeKeySpec = new PBEKeySpec(new String(password).toCharArray(), SymmetricKeyEncryptor
				.getInstance().getSalt(), SymmetricKeyEncryptor.getInstance()
				.getCount());
		try {
			keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
			FileOutputStream fos = new FileOutputStream("symm.key");
			fos.write(pbeKey.getEncoded());
			fos.close();
			return pbeKey;
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// KeyGenerator generator;
		// try {
		// generator = KeyGenerator.getInstance("AES");
		// generator.init(192);
		// return generator.generateKey();
		// } catch (NoSuchAlgorithmException e) {
		// e.printStackTrace();
		// }
		return null;
	}

	public SecretKey generateSymmetricKey()
	{
		 KeyGenerator generator;
		 try {
			 generator = KeyGenerator.getInstance("DES");
			 generator.init(56);
			 return generator.generateKey();
		 } catch (NoSuchAlgorithmException e) {
		 e.printStackTrace();
		 }
		 return null;
	}
	
	public void GenerateKeyPair() throws IOException {

		KeyPairGenerator keyGen = null;
		try {
			keyGen = KeyPairGenerator.getInstance("RSA");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		keyGen.initialize(1024);
		KeyPair keyPair = keyGen.genKeyPair();
		PrivateKey privateKey = keyPair.getPrivate();
		PublicKey publicKey = keyPair.getPublic();

		// Store Public Key.
		X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
				publicKey.getEncoded());
		FileOutputStream fos = new FileOutputStream("public.key");
		fos.write(x509EncodedKeySpec.getEncoded());
		fos.close();

		// Store Private Key.
		PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
				privateKey.getEncoded());
		fos = new FileOutputStream("private.key");
		fos.write(pkcs8EncodedKeySpec.getEncoded());
		fos.close();
	}

	public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
		//Generator.getInstance().GenerateKeyPair();
		AsymmetricKeyEncryptor s = AsymmetricKeyEncryptor.getInstance();
		PrivateKey privateKey = s.loadPrivateKey("clientdata/ahmed/");
		PublicKey publicKey = s.loadPubliceKey("serverdata/ahmed/");
		byte[] encrypted = s.encrypt("heloooo".getBytes(),privateKey);
		//System.out.println(new String (encrypted));
		System.out.println(new String(s.decrypt(encrypted, publicKey)));
		
		
	}

}
